//
//  LXApplication.m
//  bgms_d
//
//  Created by lachesis-zhu on 11/10/2017.
//  Copyright © 2017 lachesis-zhu. All rights reserved.
//

#import "LXApplication.h"

@implementation LXApplication

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];
    if (!_myidleTimer) {
        [self resetIdleTimer];
    }
    
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan) {
            [self resetIdleTimer];
        }
    }
}

//重置时钟
-(void)resetIdleTimer {
    if (_myidleTimer) {
        [_myidleTimer invalidate];
    }
    
    //将超时时间由分钟转换成秒数
    int timeout = kApplicationTimeoutInMinutes * 60;
    _myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];
}

//当达到超时时间，发送 kApplicationTimeoutInMinutes通知
-(void)idleTimerExceeded {
    [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil];
}

@end
